#!/usr/bin/env python

import time,sys,cPickle
sys.path.append(sys.path[0]+'/../include')
import init

k = init.INIT()
workdir = k.WorkDir
rds = k.rds
saveout = sys.stdout
fsock = open('%s/logs/monitor.log' % workdir,'a')
sys.stdout = fsock
sys.stderr = fsock

nowtime = time.strftime('%Y-%m-%d %H:%M:%S')

try:
    with open('/tmp/monitor-daemon.txt','r') as f:
        alldata = cPickle.load(f)
        other = cPickle.load(f)
except:
    print '%s %s get params error.' % (nowtime,__file__)
    sys.exit()

inttime = int(other['starttime']) 

try:
    rds.hset('lastmonitor','time',inttime)
    rds.hset('lastmonitor','get_time','%.2f' % (time.time() - other['starttime']))
    rds.hset('lastmonitor','errorinfo',other['errorinfo'])
    rds.hset('lastmonitor','nodes',other['nodes'])
    rds.hset('lastmonitor','projects',other['projects'])
    rds.hset('lastmonitor','interval',other['interval'])
    for i in alldata:
        rds.hset('lastmonitor',i,alldata[i])
except Exception as err:
    print '%s save to rds error: %s.' % (nowtime,str(err).strip())
sys.stdout = saveout
fsock.close()
